<?php
/**
 * 国内库存（公共库存）
 * (model)
 */

namespace App\Models\Inventory;

use App\Models\Basics\SysArea;
use App\Models\Basics\SysIArea;
use App\Models\Product\ProductClassify;
use App\Models\Product\ProductSku;
use App\Util\User;
use Illuminate\Database\Eloquent\Model;

class PublicCn extends Model
{
    protected $table = 'inventory_cn';

    protected $guarded = [];

    public $timestamps = false;

    public function area(){
        return $this->belongsTo(SysArea::class,'area_id');
    }

    public function iArea(){
        return $this->belongsTo(SysIArea::class,'area_id','area_id');
    }

    public function sku(){
        return $this->belongsTo(ProductSku::class,'psku_id');
    }

    public static function getDownloadData($re){
        $user = [];
        foreach ($re as $row){
            $temp = [];
            $temp['psku_code'] = $row->sku->psku_code ?? '';
            $temp['season'] = ProductClassify::$_season[$row->sku->classify->season ?? 3];
            $temp['classify'] = $row->sku->classify->title ?? '';
            if(!array_key_exists($row->iArea->muser_id ?? '',$user)){
                $user[$row->iArea->muser_id ?? ''] = User::getUsernameById($row->iArea->muser_id ?? '');
            }
            $temp['muser'] = $user[$row->iArea->muser_id ?? ''];
            $temp['instant'] = $row->instant;
            $temp['transit'] = $row->transit;
            $temp['reserve'] = $row->reserve;
            $temp['psku_cn'] = $row->sku->psku_cn ?? '';
            yield $temp;
        }
    }

    public static function getInstantPSkuId($pSkuIds){
        return self::query()
            ->whereIn('psku_id',$pSkuIds)
            ->whereHas('area', function ($query){
                $query->where('area', 'PUBLIC');
            })
            ->pluck('instant','psku_id')
            ->toArray();
    }

    public static function getPublicCnWithLock($pSkuId){
        return self::query()
            ->whereHas('area', function ($query){
                $query->where('area', 'PUBLIC');
            })
            ->where('psku_id',$pSkuId)
            ->lockForUpdate()
            ->first();
    }

    public static function getPublicCn($pSkuId){
        return self::query()
            ->whereHas('area', function ($query){
                $query->where('area', 'PUBLIC');
            })
            ->where('psku_id',$pSkuId)
            ->first();
    }

    public static function getInstant($pSkuId){
        $instant = self::query()
            ->whereHas('area', function ($query){
                $query->where('area', 'PUBLIC');
            })
            ->where('psku_id',$pSkuId)
            ->value('instant');
        return $instant ?: 0;
    }

    public static function getTransit($pSkuId){
        $transit = self::query()
            ->whereHas('area', function ($query){
                $query->where('area', 'PUBLIC');
            })
            ->where('psku_id',$pSkuId)
            ->value('transit');

        return $transit ?: 0;
    }

    public static function getReserve($pSkuId){
        $reserve = self::query()
            ->whereHas('area', function ($query){
                $query->where('area', 'PUBLIC');
            })
            ->where('psku_id',$pSkuId)
            ->value('reserve');
        return $reserve ?: 0;
    }

    /**
     * 获取品类下的公共在库
     * @param $sku
     * @param $classifyId
     * @param $season
     * @return int|mixed
     */
    public static function getInstantByClassify($classifyId){
        return self::query()
            ->whereHas('area', function ($query){
                $query->where('area', 'PUBLIC');
            })
            ->whereHas('sku', function ($query) use($classifyId){
                $query->where('classify_id', $classifyId);
            })
            ->sum('instant');
    }

    /**
     * 获取公共库存的汇总
     * @param $sku
     * @param $classifyId
     * @param $season
     * @param $reportStatus
     * @return int|mixed
     */
    public static function getInstantSum($sku = null, $classifyId = null, $season = null, $reportStatus = null){
        $query = self::query()
            ->whereHas('area', function ($query){
                $query->where('area', 'PUBLIC');
            });
        if ($sku) {
            $query->whereHas('sku', function ($query) use($sku){
                $query->where('psku_code', 'like', "%{$sku}%")
                    ->orWhere('psku_cn', 'like', "%{$sku}%");
            });
        }
        if ($classifyId) {
            $query->whereHas('sku', function ($query) use($classifyId){
                $query->where('classify_id', $classifyId);
            });
        }
        if ($season) {
            $query->whereHas('sku.classify', function ($query) use($season){
                $query->where('season', $season);
            });
        }
        if ($reportStatus) {
            $query->whereHas('sku.classify', function ($query) use($reportStatus){
                $query->where('report_status', $reportStatus);
            });
        }
        return $query->sum('instant');
    }
}
